package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by Czl on 2015/7/25.
 */
@SuppressWarnings("serial")
public class CourseStudentStatus extends Model<CourseStudentStatus> {

    public static final CourseStudentStatus dao = new CourseStudentStatus();

    /**
     * 保存
     * @param courseStudentStatus
     * @param courseId
     * @param studentId
     * @param type
     */
    public void save(CourseStudentStatus courseStudentStatus,Long courseId,Long studentId,String type){
        courseStudentStatus.set("course_id",courseId);
        courseStudentStatus.set("student_id",studentId);
        courseStudentStatus.set("target_type",type);
        courseStudentStatus.remove("id").save();
    }

    /**
     * 根据课程ID,学员ID,类型
     * 查询课程详情的状态
     * @param courseId
     * @param studentId
     * @return
     */
    public CourseStudentStatus getCourseStudentStatus(Long courseId, Long studentId, String type){
        return dao.findFirst("select * from t_course_student_status " +
                "where course_id =? and student_id = ? and target_type = ?", courseId, studentId, type);
    }

    /**
     * 分页下旬我的收藏，我的下载，我的点赞等
     * @param pageNumber
     * @param pageSize
     * @param paras
     * @return
     */
    public Page<CourseStudentStatus> getCourseCollectionPage(int pageNumber,int pageSize,String sql,Object[] paras){
        return dao.paginate(pageNumber,pageSize,"select t.* ",sql,paras);
    }

    /**
     * 删除我的收藏
     * @param studentId
     * @param courseId
     * @param type
     * @return
     */
    public int deleteMyCollection(Long studentId,Long courseId,String type){
      return Db.update("delete from t_course_student_status " +
              "where course_id =? and student_id = ? and target_type = ?",courseId,studentId,type);
    }


}
